package com.taxx.controller;

import com.taxx.common.Result;
import com.taxx.controller.dto.TaxIdentityReportReqDTO;
import com.taxx.controller.dto.TaxIncomeReportReqDTO;
import com.taxx.controller.dto.TaxPlatformReportReqDTO;
import com.taxx.controller.dto.TaxIdentityCancelReqDTO;
import com.taxx.controller.dto.TaxIncomeCancelReqDTO;
import com.taxx.controller.dto.TaxPlatformCancelReqDTO;
import com.taxx.service.TaxReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 税务报送控制器
 */
@RestController
@RequestMapping("/tax/report")
@CrossOrigin
public class TaxReportController {
    
    @Autowired
    private TaxReportService taxReportService;
    
    /**
     * 收入信息报送
     */
    @PostMapping("/income")
    public Result<Map<String, Object>> incomeReport(@RequestBody TaxIncomeReportReqDTO incomeReportReqDTO) {
        try {
            Map<String, Object> result = taxReportService.reportIncomeData(incomeReportReqDTO);
            return Result.success("收入信息报送完成", result);
        } catch (Exception e) {
            return Result.error("收入信息报送失败: " + e.getMessage());
        }
    }
    
    /**
     * 身份信息报送
     */
    @PostMapping("/identity")
    public Result<Map<String, Object>> identityReport(@RequestBody TaxIdentityReportReqDTO identityReportReqDTO) {
        try {
            Map<String, Object> result = taxReportService.reportIdentityData(identityReportReqDTO);
            return Result.success("身份信息报送完成", result);
        } catch (Exception e) {
            return Result.error("身份信息报送失败: " + e.getMessage());
        }
    }
    
    /**
     * 平台信息报送
     */
    @PostMapping("/platform")
    public Result<Map<String, Object>> platformReport(@RequestBody TaxPlatformReportReqDTO platformReportReqDTO) {
        try {
            Map<String, Object> result = taxReportService.reportPlatformData(platformReportReqDTO);
            return Result.success("平台信息报送完成", result);
        } catch (Exception e) {
            return Result.error("平台信息报送失败: " + e.getMessage());
        }
    }
    
    /**
     * 身份信息作废
     */
    @PostMapping("/identity/cancel")
    public Result<Map<String, Object>> identityCancel(@RequestBody TaxIdentityCancelReqDTO cancelReqDTO) {
        try {
            Map<String, Object> result = taxReportService.cancelIdentityData(cancelReqDTO);
            return Result.success("身份信息作废完成", result);
        } catch (Exception e) {
            return Result.error("身份信息作废失败: " + e.getMessage());
        }
    }
    
    /**
     * 收入信息作废
     */
    @PostMapping("/income/cancel")
    public Result<Map<String, Object>> incomeCancel(@RequestBody TaxIncomeCancelReqDTO cancelReqDTO) {
        try {
            Map<String, Object> result = taxReportService.cancelIncomeData(cancelReqDTO);
            return Result.success("收入信息作废完成", result);
        } catch (Exception e) {
            return Result.error("收入信息作废失败: " + e.getMessage());
        }
    }
    
    /**
     * 平台信息作废
     */
    @PostMapping("/platform/cancel")
    public Result<Map<String, Object>> platformCancel(@RequestBody TaxPlatformCancelReqDTO cancelReqDTO) {
        try {
            Map<String, Object> result = taxReportService.cancelPlatformData(cancelReqDTO);
            return Result.success("平台信息作废完成", result);
        } catch (Exception e) {
            return Result.error("平台信息作废失败: " + e.getMessage());
        }
    }
    
    /**
     * 重试失败报送
     */
    @PostMapping("/retry")
    public Result<Map<String, Object>> retryFailedReport(@RequestParam String dataType) {
        try {
            Map<String, Object> result = taxReportService.retryFailedReport(dataType);
            return Result.success("重试失败报送完成", result);
        } catch (Exception e) {
            return Result.error("重试失败报送失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取报送状态统计
     */
    @GetMapping("/status")
    public Result<Map<String, Object>> getReportStatus() {
        try {
            Map<String, Object> status = taxReportService.getReportStatistics();
            return Result.success("获取报送状态成功", status);
        } catch (Exception e) {
            return Result.error("获取报送状态失败: " + e.getMessage());
        }
    }


    @GetMapping("/uuid")
    public Result<String> getPtUUID() {
        try {
            String data = taxReportService.getPtUUID();
            return Result.success("获取报送状态成功", data);
        } catch (Exception e) {
            return Result.error("获取报送状态失败: " + e.getMessage());
        }
    }
}
